/**
 * 用户授权唤起 工具
 * author bylar
 * time 2018年11月1日 16点16分
 */
const lib = {
  'scope.userInfo': '用户信息', // Disable
  'scope.userLocation': '地理位置',
  'scope.address' : '通讯地址',
  'scope.invoiceTitle' : '发票抬头',
  'scope.werun' : '	微信运动步数',
  'scope.record': '录音功能',
  'scope.writePhotosAlbum': '保存到相册',
  'scope.camera': "	摄像头",
}

/**
 * showAuthSetting 唤起用户授权设置
 */
function getAuth(scopeName, option) {
  option = option || {};
  option.fail = option.fail || function(){};
  option.success = option.success || function () {};  
  /**
   * showSetting 显示设置列表
   */
  function showSetting(){
    let modalStuct = {
      title: lib[scopeName] + '获取失败',
      content: '请在设置中打开' + lib[scopeName] + '授权',
      success: res => {
        if (res.confirm) {wx.openSetting({ success: settingCallBack }) }
        else option.fail();
      }
    }
    wx.showModal(modalStuct);
  }
  /**
   * settingCallBack 设置列表回调
   */
  function settingCallBack(res){
    if (res.authSetting[scopeName]){
      option.success();
    } else {
      option.fail();
    }
  }
  if (scopeName == "scope.userInfo") showSetting();
  else wx.authorize({ scope: scopeName, success: option.success, fail: showSetting})
}
module.exports.getAuth = getAuth;


